﻿using K8.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.PCRBW.SpiService;

public class MesLockRepositories : IMesLockRepositories
{
    #region 同步方法
    public MesLock QueryById(string objId)
    {
        var _db = Db.Default;
        return _db.MesLocks.FirstOrDefault(d => d.LockId == objId);
    }

    /// <summary>
    /// 功能描述:根据ID查询数据
    /// 作　　者:Robin
    /// </summary>
    /// <param name="lstIds">id列表,如果是联合主键，请使用Where条件</param>
    /// <returns>数据实体列表</returns>
    public List<MesLock> QueryByIDs(object[] lstIds)
    {
        var _db = Db.Default;
        return _db.MesLocks.Where(d => lstIds.Contains(lstIds)).ToList();
    }

    /// <summary>
    /// 写入实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns>插入个数 插入成功为1</returns>
    public int Add(MesLock entity)
    {
        var _db = Db.Default;
        
        return _db.MesLocks.Insert(entity);
    }

    /// <summary>
    /// 批量插入实体
    /// </summary>
    /// <param name="listEntity">实体集合</param>
    /// <returns>影响行数</returns>
    public int Add(IEnumerable<MesLock> listEntity)
    {
        var _db = Db.Default;
        _db.MesLocks.AddRange(listEntity);
        int insertedCount = _db.SaveChanges();
        return insertedCount;
    }
    /// <summary>
    /// 根据实体删除一条数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns></returns>
    public int DeleteById(string id)
    {
        var _db = Db.Default;
        return _db.MesLocks.Where(d => d.LockId == id).Delete();
    }

    /// <summary>
    /// 删除指定ID集合的数据(批量删除)
    /// </summary>
    /// <param name="ids">主键ID集合</param>
    /// <returns></returns>
    public int Deletes(IEnumerable<MesLock> entitys)
    {
        try
        {
            var _db = Db.Default;
            _db.MesLocks.BulkDelete(entitys);
            int result = _db.SaveChanges();
            return result;
        }
        catch (Exception ex)
        {
            return 0;
        }
    }
    /// <summary>
    /// 更新实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns></returns>
    public int Update(MesLock entity)
    {
        var _db = Db.Default;
        return _db.MesLocks.Update<MesLock>(entity);
    }
    /// <summary>
    /// 待条件更新所有数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Update(Expression<Func<MesLock, bool>> whereExpression, MesLock entity)
    {
        var _db = Db.Default;
        return _db.MesLocks.Where(whereExpression).Update(new MesLock()
        {
            BatchName = entity.BatchName,
            PartNumber=entity.PartNumber,
            PassFlag=entity.PassFlag,
            UpdateUser = entity.UpdateUser,
            Remarks = entity.Remarks
        });
    }

    /// <summary>
    /// 正序查询第一条数据
    /// </summary>
    /// <returns></returns>
    public MesLock First()
    {
        var _db = Db.Default;
        return _db.MesLocks.First();
    }
    /// <summary>
    /// 查询第一条数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public MesLock First(Expression<Func<MesLock, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.MesLocks.Where(whereExpression).OrderBy(d => d.CreateTime).FirstOrDefault(whereExpression);
    }


    /// <summary>
    /// 功能描述:查询所有数据
    /// 作　　者:Robin
    /// </summary>
    /// <returns>数据列表</returns>
    public List<MesLock> Query()
    {
        try
        {
            var _db = Db.Default;
            return _db.MesLocks.ToList();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Query执行异常:{ex.Message}");
        }
        return null;
    }


    /// <summary>
    /// 功能描述:查询数据列表
    /// 作　　者:Robin
    /// </summary>
    /// <param name="whereExpression">whereExpression</param>
    /// <returns>数据列表</returns>
    public List<MesLock> Query(Expression<Func<MesLock, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.MesLocks.Where(whereExpression).ToList();
    }


    /// <summary>
    /// 根据sql语句查询
    /// </summary>
    /// <param name="strSql">完整的sql语句</param>
    /// <param name="parameters">参数</param>
    /// <returns>泛型集合</returns>
    public List<MesLock> QuerySql(string strSql, Parameter[] parameters = null)
    {
        var _db = Db.Default;
        var result = _db.MesLocks.FromSqlRaw(strSql, parameters).ToList();

        return result;
    }
    /// <summary>
    /// 根据sql语句查询 执行update、delete、add
    /// </summary>
    /// <param name="strSql">完整的sql语句</param>
    /// <returns>泛型集合</returns>
    public int ExecuteSqlRaw(string strSql, Parameter[] parameters)
    {
        var dbHelper = new DbHelper();
        dbHelper.CommandText = strSql.ToString();
        dbHelper.CommandType = System.Data.CommandType.Text;
        for (int i = 0; i < parameters.Length; i++)
        {
            dbHelper.AddParameter(parameters[i]);
        }
        int rowsReturned = dbHelper.ExecuteNonQuery();

        return rowsReturned;
    }

    #endregion
}
